package com.isczy.eduservice.controller;


import com.isczy.eduservice.entity.EduChapter;
import com.isczy.eduservice.service.EduChapterService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程小章节 前端控制器
 * </p>
 *
 * @author CZY
 * @since 2021-01-02
 */
@Api(description = "课程小章节 前端控制器")
@RestController
@RequestMapping("/eduservice/edu-chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /***
     * ***************************************
     * 功能描述:课程大纲列表,根据课程id进行查询
     * @param courseId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:07】
     * @author modify:
     */
    @ApiOperation(value = "根据课程id查询课程大纲列表")
    @GetMapping("getChapterVideo/{courseId}")
    public DataResult getChapterVideo(@PathVariable String courseId) {
        try {
            return DataResult.builder()
                    .data(chapterService.getChapterVideoByCourseId(courseId))
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据课程id进行章节查询失败")
                    .params(courseId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:添加章节
     * @param eduChapter
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:07】
     * @author modify:
     */
    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public DataResult addChapter(@RequestBody EduChapter eduChapter) {
        try {
            chapterService.save(eduChapter);
            return DataResult.builder()
                    .message("添加章节成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("添加章节失败")
                    .params(eduChapter)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据章节id查询
     * @param chapterId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:07】
     * @author modify:
     */
    @ApiOperation(value = "根据章节id查询")
    @GetMapping("getChapterInfo/{chapterId}")
    public DataResult getChapterInfo(@PathVariable String chapterId) {
        try {
            return DataResult.builder()
                    .data(chapterService.getById(chapterId))
                    .message("根据章节id查询成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据章节id查询失败")
                    .params(chapterId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:修改章节
     * @param eduChapter
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:07】
     * @author modify:
     */
    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public DataResult updateChapter(@RequestBody EduChapter eduChapter) {
        try {
            chapterService.updateById(eduChapter);
            return DataResult.builder()
                    .message("修改章节成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("修改章节失败")
                    .params(eduChapter)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:删除章节
     * @param chapterId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:07】
     * @author modify:
     */
    @ApiOperation(value = "删除章节")
    @DeleteMapping("{chapterId}")
    public DataResult deleteChapter(@PathVariable String chapterId) {
        try {
            int count = chapterService.deleteChapter(chapterId);
            if (count >0){
                return DataResult.builder()
                        .message("该章节存在"+count+"个小节，不允许被删除！")
                        .errorBuild();
            }else{
                return DataResult.builder()
                        .message("删除章节成功")
                        .successBuild();
            }
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("删除章节失败")
                    .params(chapterId)
                    .build();
        }
    }
}

